﻿using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

namespace Simhopp.Client
{
    /// <summary>
    /// Interaction logic for ClientWindow.xaml
    /// </summary>
    public partial class ClientWindow : Window
    {
        public ClientWindow()
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            this.Closing += EventClose;
            Application.Current.MainWindow = this;
            InitializeComponent();
            this.InputBindings.Add(new KeyBinding(Presenter.LogOn, new KeyGesture(Key.L, ModifierKeys.Control)));
            this.InputBindings.Add(new KeyBinding(Presenter.LogOut, new KeyGesture(Key.O, ModifierKeys.Control)));
            this.CommandBindings.AddRange((this.FindResource("myPresenter") as Presenter).CommandBindings);
            
        }

        /// <summary>
        /// Executed when the window closes. 
        /// </summary>
        /// <param name="sender">The calling object</param>
        /// <param name="e">The event arguments</param>
        private void EventClose(object sender, EventArgs e)
        {
            var myPresenter = this.FindResource("myPresenter");
            (myPresenter as Presenter).ExitGraceful();
        }

        /// <summary>
        /// Global catch-all for errors. 
        /// </summary>
        /// <param name="sender">the invoking object</param>
        /// <param name="e">the exception argument</param>
        void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            MessageBox.Show(String.Format("Uncaught error: \n{0}\n{1}\n{2}", sender.GetType(), e.ExceptionObject, e.ToString()));
            
        }

        /// <summary>
        /// Executed when the window is closing.
        /// </summary>
        /// <param name="e">The event arguments</param>
        protected override void OnClosing(CancelEventArgs e)
        {
            e.Cancel = true;
            Environment.Exit(0);
        }

  
    }
}
